package server.controller;

import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import plug.result.RetResponse;
import plug.result.RetResult;
import server.dao.FrmDictionDao;
import server.model.entity.FrmDiction;
import server.model.entity.Users;
import server.service.PermissionsService;

/**
 * 创建人:    xvzhe
 * 创建时间:
 * 修改时间:
 * 版本:      v1.0
 */
@RestController
@RequestMapping("v1/api/diction")
public class DictionController {
    @Autowired
    FrmDictionDao dictionDao;
    @Autowired
    PermissionsService permissionsService;

    @GetMapping("/{name}")
    @CrossOrigin(origins = "*")
    @ApiOperation("key=val")
    public RetResult find(@PathVariable String name) {
        return RetResponse.makeOKRsp(dictionDao.findByKeyst(name));
    }

    @PostMapping
    @CrossOrigin(origins = "*")
    @ApiOperation("key=val存在")
    public RetResult save(@RequestBody FrmDiction diction) {
        Users users = (Users) SecurityUtils.getSubject().getSession().getAttribute("Users");
        if (!users.getRole().getId().equals("1")) {
            return RetResponse.makeErrRsp("error");
        }
        FrmDiction d = dictionDao.findByKeyst(diction.getKeyst());
        if (d != null) {
            d.setVal(diction.getVal());
            dictionDao.save(d);
            return RetResponse.makeOKRsp();
        }
        dictionDao.save(diction);
        return RetResponse.makeOKRsp();
    }
}
